package org.delphy.indy2.controls
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.geom.Matrix;
	
	import mx.controls.TabBar;
	import mx.styles.StyleManager;
	
	import org.delphy.indy2.core.ICarbonComponent;

	//--------------------------------------
	//  Styles
	//--------------------------------------
	
	/**
	 * Corner radius
	 * 
	 * @default 24
	 */
	[Style(name="cornerRadius", type="Number", inherit="no")]
	
	/**
	 * Backbround Colors
	 * 
	 * @default #333333, #000000
	 */
	[Style(name="backgroundColors", type="Array", arrayType="Number", inherit="no")]
	
	/**
	 * Backbround Alphas
	 */
	[Style(name="backgroundAlphas", type="Array", arrayType="Number", inherit="no")]
	
	/**
	 * Backbround Ratios
	 */
	[Style(name="backgroundRatios", type="Array", arrayType="Number", inherit="no")]
	
	/**
	 * Backbround RatioUnits
	 */
	[Style(name="backgroundRatioUnits", type="Array", arrayType="String", inherit="no")]
	
	public class CarbonTabBar extends TabBar implements ICarbonComponent
	{
		
		/**
		 * Constructor
		 */		
		public function CarbonTabBar()
		{
			super();
		}

		/**
		 * @protected
		 */
		override protected function commitProperties() : void{
			super.commitProperties();
			
			// Fallback to default skin
			if(!styleName)
				styleName="carbonTabBar";			
		}
		
		/**
		 * @protected
		 */
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
			// Pass styles to skin
			var cornerRadius:Number=getStyle("cornerRadius");
			if(!cornerRadius){
				cornerRadius=StyleManager.getStyleDeclaration(".carbonTabBar").getStyle("cornerRadius");
				setStyle("cornerRadius", cornerRadius);
			}
			
			// Manually draw the barbar
			var fillColors:Array;
			if(getStyle("backgroundColors"))
				fillColors=getStyle("backgroundColors");
			else
				fillColors=StyleManager.getStyleDeclaration(".carbonTabBar").getStyle("backgroundColors");		
			
			var g:Graphics=graphics;
			var mat:Matrix=new Matrix();
			mat.createGradientBox(unscaledWidth,unscaledHeight,Math.PI/2);
			
			g.clear();
			g.beginGradientFill(GradientType.LINEAR,[fillColors[0],fillColors[1]],[1,1],[0,255],mat);
			g.drawRoundRect(0,0,unscaledWidth,unscaledHeight,cornerRadius,cornerRadius);
			g.endFill();
			
			// Highlight
			mat.createGradientBox(unscaledWidth-4,unscaledHeight/2,Math.PI/2);
			g.beginGradientFill(GradientType.LINEAR,[0xFFFFFF,0xFFFFFF],[0.5,0.1],[0,255],mat);
			g.drawRoundRect(2,0,unscaledWidth-4,unscaledHeight/2,cornerRadius,cornerRadius);
			g.endFill();		
		}
		
		//--------------------------------------
		//  Implementation of ICarbonComponent
		//--------------------------------------		
		
		/**
		 * @public
		 */
		public function get defaultStyleName():String{
			return "carbonTabBar";
		}		
	}
}